// 用于切换暗黑模式的工具类
const darkTheme = {
  _darkTheme: 'theme',
  // 初始化暗黑模式
  // 优先级：本地存储 > 系统默认
  isDark: false,
  initTheme() {
    const savedMode = localStorage.getItem(this._darkTheme)
    if (savedMode !== null) {
      this.isDark = savedMode === 'dark'
    } else {
      this.isDark = window.matchMedia('(prefers-color-scheme: dark)').matches
    }
    this.applyMode()
  },

  // 应用暗黑模式
  applyMode() {
    const html = document.documentElement
    if (this.isDark) {
      html.classList.add('tw-dark')
      html.classList.remove('tw-light')
      html.setAttribute('data-theme', 'dark')
    } else {
      html.classList.remove('tw-dark')
      html.classList.add('tw-light')
      html.setAttribute('data-theme', 'light')
    }
    localStorage.setItem(this._darkTheme, this.isDark ? 'dark' : 'light')
  },

  // 切换暗黑模式
  toggleMode() {
    this.isDark = !this.isDark
    this.applyMode()
  },
}
export default darkTheme
